package com.tanhua.sso.service;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.pojo.User;
import com.tanhua.sso.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * TODO
 *
 * @author zZZFf
 * @since 2021/7/11 10:02
 */
@Service
public class MyCenterService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 修改用户个人头像
     *
     * @param headPhoto 头像文件
     * @return
     */
    public Boolean updateUserLogo(MultipartFile headPhoto, String token) {
        return userInfoService.saveUserLogo(headPhoto, token);
    }

    /**
     * 更改手机号
     * 第一步：发送短信验证码
     *
     * @return Boolean
     */
    public Boolean sendVerificationCode(String token) {
        // 校验token
        User user = this.userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }

        ErrorResult errorResult = this.smsService.sendCheckCode(user.getMobile());
        if (ObjectUtil.isEmpty(errorResult)) {
            return true;
        }

        return false;
    }

    /**
     * 更改手机号
     * 第二步：校验验证码
     *
     * @param token            令牌
     * @param verificationCode 要检验的验证码
     * @return Boolean
     */
    public Boolean checkVerificationCode(String token, String verificationCode) {
        // 校验token
        User user = this.userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }

        // 获得存入Redis中的验证码
        String redisKey = "CHECK_CODE_" + user.getMobile();
        if (StringUtils.equals(verificationCode, this.redisTemplate.opsForValue().get(redisKey))) {
            // 校验成功，验证码校验完成后需要删除
            // 删除验证码
            this.redisTemplate.delete(redisKey);

            return true;
        }

        return false;
    }

    /**
     * 更改手机号
     * 第三步：保存手机号
     *
     * @param token          令牌
     * @param newPhoneNumber 要保存的新手机号
     * @return Boolean
     */
    public Boolean saveNewPhoneNumber(String token, String newPhoneNumber) {
        // 校验token
        User user = this.userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }

        // 判断该手机号是否已注册
        if(this.userService.hasNewPhoneNumber(newPhoneNumber)){
            // 该手机号已注册
            return false;
        }

        Boolean result = this.userService.updatePhoneNumber(user.getId(), newPhoneNumber);

        if (result) {
            // 更新成功，将Redis中缓存的手机号删除
            String redisKey = "TANHUA_USER_MOBILE_" + user.getId();
            this.redisTemplate.delete(redisKey);

            return true;
        }

        return false;
    }
}
